package com.ys.config;

import com.ys.vo.R;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常捕获
 */
@ControllerAdvice
public class GlobalException {
    private static final Logger logger = LoggerFactory.getLogger(GlobalException.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R exception(Exception e) {
        logger.error("系统全局发生错误:",e.getCause(),e);
        if (e instanceof AuthorizationException){
            if (e instanceof UnauthenticatedException){
                return R.error("请重新登录");
            }
            return R.error("尚未配置该权限");
        }
        return R.error("系统发生错误");
    }
}
